// SpriteBatch.cs
//

using System;
using System.Collections.Generic;
using System.Html.Media.Graphics;
using System.Html;
using Moonbeam.Content;

namespace Moonbeam.Core
{
    public class SpriteBatch
    {
        CanvasContext2D context;

        public SpriteBatch()
        {           
            context = ((GraphicsDevice)GameComponentManager.Instance.GetComponent(typeof(GraphicsDevice))).GetDrawingSurface();
        }

        public void DrawText(string text, Vector2 postition, SpriteFont font, float scale, double rotateAngle, int layer, CompositeOperation bendMode)
        {
            context.Save();
            
            context.FillStyle = font.FillStyle;
            context.Font = font.FontString;
            context.TextBaseline = font.TextBaseline;

            if (rotateAngle != 0)
            {
                TextMetrics tm = context.MeasureText(text);

                context.Translate(postition.x + (0.5 * tm.Width), postition.y + (0.5 * font.FontSizeInPixels));
                context.Rotate((Math.PI / 180) * rotateAngle);

                postition.x = 0;
                postition.y = 0;
            }
           
            context.FillText(text, postition.x, postition.y);

            context.Restore();
        }

        public void DrawSprite(Texture t, Vector2 postition, Vector2 scale, double rotateAngle, int layer, CompositeOperation bendMode)
        {
            context.Save();

            //context.Scale(scale.x, scale.y);

            if (rotateAngle != 0)
            {
                context.Translate(postition.x + (0.5 * (t.Width*scale.x)), postition.y + (0.5 * (t.Height*scale.y)));
                context.Rotate((Math.PI / 180) * rotateAngle);

                postition.x = 0;
                postition.y = 0;
            }


            context.DrawImage(t.GetIamge(), postition.x, postition.y, t.Width * scale.x, t.Height * scale.y);

            context.Restore();       
            
        }        

        public void HelloWorld()
        {
            context.TextBaseline = TextBaseline.Top;
            context.Font = "40px";
            context.FillStyle = Colour.Red1;
            context.FillText("Hello World!", 10, 10);
        }
    }
}
